<?php
namespace App\Services\IntegralOrder;
use App\Services\IntegralOrder\Order\OrderLogic;
use App\Services\User\Token;
use App\Services\User\App;
use App\Services\User\User;
use App\Services\Fund\FundAccount;
use Request;
/**
 * Class Order Order服务
 * @package App\Services\Order
 */
class Order
{
    
    /*积分兑换商品*/
  /*  public function integralExchange(): array 
    {
        $data = $this->haveIssetReceiveData(['goods_id'=>0, 'user_id'=>0, 'number'=>0, 'order_code'=>' ', 'pick_up_code'=>' ', 'name'=>' ', 'phone'=>' ', 'address'=>'', 'remark'=>'']);
        return $this->stepRun($data, 'integralExchange');
    }*/

    /*获取我的积分兑换订单*/
   /* public function integralOrderList(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'page'=>1, 'listRows'=>15, 'keywords'=>'']);
        return $this->stepRun($data, 'integralOrderList');
    }*/

    /*获取我的某个兑换订单详情*/
    /*public function integralOrderInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'integralOrderInfo');
    }*/

    /*后台获取积分订单列表*/
    public function adminIntegralOrderList($data): array 
    {
        $data = init_array($data,['page'=>1, 'listRows'=>15, 'keywords'=>'']);
        ValidateOrder::checkValidate($data, ['page','listRows']);
        return (new OrderLogic())->adminIntegralOrderList($data);

        /*$data = $this->haveIssetReceiveData(['page'=>1, 'listRows'=>15, 'keywords'=>'']);
        return $this->stepRun($data, 'adminIntegralOrderList');*/
    }

    /*后台获取某积分订单的详情*/
    public function adminIntegralOrderInfo($data): array 
    {
        $data = init_array($data,['id'=>0]);
        ValidateOrder::checkValidate($data, ['id']);
        return (new OrderLogic())->adminIntegralOrderInfo($data);
    }

    /*后台对某积分订单进行发货操作*/
    public function shipmentsGoods($data): array 
    {
        $app_key = config('tpproject.yayi_admin_app_key');
        $token = (new Token())->hGetAll($data['token']);
        if($token['res']['status'] != SUCCESS_STATUS){
            return $token;
        }
        $find = (new App())->find(['app_key'=>$app_key]);
        if($find['status'] != SUCCESS_STATUS){
            return $find ;
        }
        $adminIntegralOrderInfo = (new OrderLogic())->adminIntegralOrderInfo(['id'=>$data['id']]);
        $userInfo = (new User())->info(['id'=>$adminIntegralOrderInfo['res']['user_id']]);
        if($userInfo['status'] != SUCCESS_STATUS){
            return $userInfo;
        }
        //解冻用户积分 + 用户积分转账平台积分 + 进行发货操作
        $unfreeze['user_id'] = $transfer['user_id'] = $token['res']['id'];
        $unfreeze['app_id'] = $transfer['app_id'] = $find['res']['id'];
        $unfreeze['opposite_id'] = $transfer['opposite_id'] = $adminIntegralOrderInfo['res']['id'];
        $unfreeze['integral'] = $transfer['integral'] = bcmul($adminIntegralOrderInfo['res']['integral'], $adminIntegralOrderInfo['res']['number']);
        $unfreeze['update_ip'] = $transfer['update_ip'] = Request::getClientIp();
        $unfreeze['type'] = 'integral_order';
        $unfreeze['obj_id'] = $adminIntegralOrderInfo['res']['id'];
        $unfreeze['freeze_counts'] = 1;
        $unfreeze['user_info'] = $transfer['user_info'] = $userInfo['res'];
        $unfreeze['opposite_info'] = $transfer['opposite_info'] = $token['res'];
        $shipmentsGoods['id'] = $adminIntegralOrderInfo['res']['id'];
        $shipmentsGoods['logistics_company_code'] = $data['logistics_company_code'];
        $shipmentsGoods['logistics_company_name'] = $data['logistics_company_name'];
        $shipmentsGoods['waybill_number'] = $data['waybill_number'];
        $unfreeze = (new FundAccount())->unfreeze($unfreeze);
        $transfer = (new FundAccount())->transfer($transfer);
        $data = init_array($shipmentsGoods,['id'=>0, 'logistics_company_code'=>'', 'logistics_company_name'=>' ', 'waybill_number'=>' ']);
        ValidateOrder::checkValidate($shipmentsGoods, ['id','logistics_company_code','logistics_company_name','waybill_number']);
        return (new OrderLogic())->shipmentsGoods($shipmentsGoods);
    }

    /*我的积分订单 - 确认收货*/
   /* public function affirmPick(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'order_id'=>0]);
        return $this->stepRun($data, 'affirmPick');
    }*/





}